package com.ljb.android.comm.img.core

import android.annotation.SuppressLint
import android.graphics.Bitmap
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.util.*

class ImgFormatFactory {

    companion object {

        @JvmStatic
        var mCache = WeakHashMap<ImgFormat, RequestOptions>()

        @SuppressLint("CheckResult")
        @JvmStatic
        fun foundOptionByFormatAndScale(
            format: ImgFormat,
            scaleMode: Transformation<Bitmap>
        ): RequestOptions {
            var options = mCache[format]
            if (options == null) {
                options = when (format) {
                    ImgFormat.Round -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.ALL
                                )
                            )
                        )
                    }
                    ImgFormat.RoundTop -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.TOP
                                )
                            )
                        )
                    }
                    ImgFormat.RoundBottom -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.BOTTOM
                                )
                            )
                        )
                    }
                    ImgFormat.RoundLeft -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.LEFT
                                )
                            )
                        )
                    }
                    ImgFormat.RoundRight -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.RIGHT
                                )
                            )
                        )
                    }
                    ImgFormat.RoundTopLeft -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.TOP_LEFT
                                )
                            )
                        )
                    }
                    ImgFormat.RoundTopRight -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.TOP_RIGHT
                                )
                            )
                        )
                    }
                    ImgFormat.RoundBottomLeft -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.BOTTOM_LEFT
                                )
                            )
                        )
                    }
                    ImgFormat.RoundBottomRight -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                RoundedCornersTransformation(
                                    format.value,
                                    0,
                                    RoundedCornersTransformation.CornerType.BOTTOM_RIGHT
                                )
                            )
                        )
                    }
                    ImgFormat.Circle -> {
                        RequestOptions.circleCropTransform()
                    }
                    ImgFormat.Blur -> {
                        RequestOptions.bitmapTransform(
                            MultiTransformation(
                                scaleMode,
                                BlurTransformation()
                            )
                        )
                    }
                    else -> {
                        RequestOptions.bitmapTransform(scaleMode)
                    }
                }
                options.diskCacheStrategy(DiskCacheStrategy.ALL)
                mCache[format] = options
            }
            return options
        }
    }
}